package org.uim.cricket.analyzer.parsers.match.impl;

import org.uim.cricket.analyzer.parsers.match.MatchResultParsable;
import org.uim.cricket.entities.match.CricketMatch;
import org.uim.cricket.entities.match.MatchResult;
import org.uim.cricket.entities.match.ResultType;
import org.uim.cricket.entities.match.ResultWonType;

public class DefaultMatchResultParser implements MatchResultParsable {

	private final String MATCH_TIED_TEXT = "match tied";
	private final String MATCH_DRAWN_TEXT = "match drawn";
	private final String MATCH_NORESULT_TEXT = "no result";
	
	@Override
	public MatchResult parseMatchResult(String source, CricketMatch match) {
		MatchResult res = new MatchResult();
		source = source.trim();
		
		res.setResultType(ResultType.ABONDANT);
		if (source.indexOf("(D/L") >= 0) {
			res.setByDuckworthLewis(true);
		}

		int pos = source.indexOf(" won by ");
		if (pos > 0) {
			res = defineWinOrLost(source, pos, res);
		} else {
			
			pos = source.indexOf(" won ");
			if (pos < 0) {
				source = source.toLowerCase();
				if (source.startsWith(MATCH_TIED_TEXT)) {
					res.setResultType(ResultType.TIED);
				} else if (source.startsWith(MATCH_DRAWN_TEXT)) {
					res.setResultType(ResultType.DRAWN);
				} else if (source.startsWith("match cancelled")
						|| source.startsWith("match abandoned")) {
					res.setResultType(ResultType.ABONDANT);
				} else if (source.startsWith(MATCH_NORESULT_TEXT)) {
					res.setResultType(ResultType.NO_RESULT);
				}
			} else {
				
				res.setWonTeam(source.substring(0, pos).trim());
				res.setResultType(ResultType.DESICIVE);
				res.setWonType(ResultWonType.BY_DAFAULT);
				
			}
			
			

		}
		
		return res;
	}

	private MatchResult defineWinOrLost(String source, int wonByPos, MatchResult res) {
		int pos = wonByPos;
		res.setWonTeam(source.substring(0, pos));
		res.setResultType(ResultType.DESICIVE);
		source = source.substring(pos + 8).trim();
		
		if (source.startsWith("an innings and ")) {
		    res.setByInnings(true);
		    source = source.substring("an innings and ".length());
		}
		
		if ((pos = source.indexOf(" wicket")) > 0) {
			res.setMargin(Integer.parseInt(source.substring(0, pos)));
			res.setWonType(ResultWonType.BY_WICKETS);
		} else if ((pos = source.indexOf(" run")) > 0) {
			res.setMargin(Integer.parseInt(source.substring(0, pos)));
			res.setWonType(ResultWonType.BY_RUNS);
		} else if (source.startsWith("default")) {
			res.setWonType(ResultWonType.BY_DAFAULT);
		}

		pos = source.indexOf('(');
		if (pos > 0) {
			pos++;
			int epos = source.indexOf(')', pos);
			String content = source.substring(pos, epos);
			res.setRemainingBalls(CalculateRemainedBalls(content));
		}

		return res;
	}

	private int CalculateRemainedBalls(String src) {
		if (src.startsWith("with ")) {
			int pos = src.indexOf(' ', 5);
			if (pos > 0) {
				return Integer.parseInt(src.substring(5, pos));
			}
		}
		return -1;
	}

}
